<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/2/20
 * Time: 13:41
 */
namespace Common\Logic;

use Common\Model\ChannelModel;
use Common\Util\ErrCode;

class ChannelLogic {

    /**
     * 添加导航栏目
     * @param $params
     * @param $response
     * @return bool
     */
    public function add($params,$response){
        $ChannelModel = new ChannelModel();
        $unique = $ChannelModel->getChannelByTitle($params['title']);
        if($unique){
            $response(ErrCode::CHANNEL_NAME_ERROR);
        }
        $data = array(
            'title' => $params['title'],
            'ename' => $params['ename'],
            'keyword' => $params['keyword'],
            'description' => $params['description'],
            'display' => $params['display'],
            'sort' => $params['sort'],
            'status' => $params['status'],
            'mould' => $params['mould']
        );
        $ChannelModel->add($data);
        return true;
    }

    /**
     * 编辑导航信息
     * @param $params
     * @param $response
     * @return bool
     */
    public function edit($params,$response){
        $ChannelModel = new ChannelModel();
        $data = array(
            'id' => $params['id'],
            'title' => $params['title'],
            'ename' => $params['ename'],
            'keyword' => $params['keyword'],
            'description' => $params['description'],
            'display' => $params['display'],
            'sort' => $params['sort'],
            'status' => $params['status'],
            'mould' => $params['mould']
        );
        $res = $ChannelModel->save($data);
        if($res === false){
            $response(ErrCode::SQL_ERROR);
        }
        return false;

    }

    /**
     * 删除导航栏目信息
     * @param $params
     * @param $response
     * @return bool
     */
    public function dropById($params,$response) {
        $ChannelModel = new ChannelModel();
        $data = array(
            'id' => array('in',$params['id']),
            'status' => ChannelModel::STATUS_DELETE
        );
        $res = $ChannelModel->save($data);
        if($res === false){
            $response(ErrCode::SQL_ERROR);
        }
        return true;
    }

    public function getChannel($params = array(),$response) {

        if($params['title']){
            $title = $params['title'];
        }
        if($params['ename']){
            $ename = $params['ename'];
        }
        if($params['mould']){
            $mould = $params['mould'];
        }
        $ChannelModel = new ChannelModel();
        $res = $ChannelModel->getChannel($title,$ename,$mould);
        if($res === false) {
            $response(ErrCode::SQL_ERROR);
        }
        return $res;
    }
}